﻿using AutoMapper;
using Entity.BaseInfo.DTO.Goods;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Goods;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class GoodsService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public GoodsService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取物料列表
         */
        public async Task<RequestResponseModel<PageResult<GoodsView>>> GetGoodsList(GoodsSearchDTO goodsSearchDTO)
        {
            //筛选
            var query = _context.GoodsView
                .WhereIF(a => a.Code.Contains(goodsSearchDTO.Code), !string.IsNullOrEmpty(goodsSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(goodsSearchDTO.Name), !string.IsNullOrEmpty(goodsSearchDTO.Name))
                .WhereIF(a => a.IsEnable == goodsSearchDTO.IsEnable, goodsSearchDTO.IsEnable != null)
                ;

            //返回列表
            var goodsList = await query
                .OrderBy(a => a.CreateTime)
                .Page(goodsSearchDTO.CurrentPage, goodsSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<GoodsView>(goodsList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增物料
         */
        public async Task<RequestResponseModel<string>> AddGoods(GoodsAddDTO goodsAddDTO)
        {
            //新增数据
            var goods = _mapper.Map<GoodsModel>(goodsAddDTO);
            goods.CreateTime = DateTime.Now;
            goods.Id = Guid.NewGuid().ToString();

            //更新表
            _context.Goods.Add(goods);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", goods.Id);
        }

        /**
         * 更新物料
         */
        public async Task<RequestResponseModel<string>> PutGoods(GoodsUpdateDTO goodsUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<GoodsModel>(_context.Goods, goodsUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", goodsUpdateDTO.Id);
            }

            //编辑数据
            var goods = _mapper.Map(goodsUpdateDTO, query);

            //更新表
            _context.Goods.Update(goods);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", goodsUpdateDTO.Id);
        }

        /**
         * 删除物料
         */
        public async Task<RequestResponseModel<string>> DeleteGoods(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<GoodsModel>(_context.Goods, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.Goods.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
